home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000…tember: Reference Library / Dev.CD Sep 00 RL Disk 1.toast / mac / Technical Documentation / Develop / develop Issue 28 / develop Issue 28 code / CurveLayout / AccurateShapesLibrary / tests / TestShapeLength.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-16  |  5.3 KB  |  249 lines  |  [TEXT/CWIE]

  1. /****************************** the test app *******************************/
  2.  
  3. #include "Types.h"
  4. #include "GXTypes.h"
  5. #include "GXMath.h"
  6. #include "GXGraphics.h"
  7. #include "GXLayout.h"
  8. #include "GXExceptions.h"
  9.  
  10. #include "AccurateShapes.h"
  11. #include "Stdio.h"
  12.  
  13. #define fix2float(x) ((double)x / 65536.0)
  14.  
  15.  
  16. gxShape MakeThePath(void);
  17. gxShape MakeThePath(void)
  18.     {
  19.         static                 gxCurve            curveGeometry = {ff(50), ff(325), ff(175), ff(150), ff(300), ff(325) };
  20.         static                 gxCurve            curveGeometry2 = {ff(300), ff(325), ff(425), ff(500), ff(550), ff(325) };
  21.         gxShape                nicePath;
  22.         gxShape                curve2;
  23.     
  24.         nicePath = GXNewCurve(&curveGeometry);
  25.         GXSetShapeType(nicePath, gxPathType);
  26.         curve2= GXNewCurve(&curveGeometry2);
  27.         GXSetShapeParts(nicePath, 0, 0, curve2, gxBreakNeitherEdit);
  28.         GXDisposeShape(curve2);
  29.         
  30.         GXSetShapeFill(nicePath, gxFrameFill);
  31.         
  32.         GXMoveShape(nicePath, ff(0), ff(-120));
  33.         
  34.         GXSetShapeType(nicePath, gxPolygonType);
  35.     
  36.         return(nicePath);    
  37.     }
  38.  
  39.  
  40.  
  41.  
  42.  
  43. typedef struct {
  44.  
  45.     gxPoint            currentPoint;                // current pen
  46.     gxPoint            firstPoint;                    // first point in countour.
  47.  
  48. } TestWalkRec;
  49.  
  50.  
  51.  
  52. Boolean    TestMoveto(gxPoint *p, TestWalkRec* pWalk);
  53. Boolean    TestMoveto(gxPoint *p, TestWalkRec* pWalk)
  54.     {
  55.         pWalk->currentPoint.x = p->x;
  56.         pWalk->currentPoint.y = p->y;
  57.         
  58.         pWalk->firstPoint.x = p->x;
  59.         pWalk->firstPoint.y = p->y;
  60.         
  61.  
  62.         return(false);
  63.     }
  64.  
  65.  
  66.  
  67. Boolean    TestLineto(gxPoint *p, TestWalkRec* pWalk);
  68. Boolean    TestLineto(gxPoint *p, TestWalkRec* pWalk)
  69.     {
  70.         gxLine            aLine;
  71.         
  72.         aLine.first.x = pWalk->currentPoint.x;
  73.         aLine.first.y = pWalk->currentPoint.y;
  74.         aLine.last.x = p->x;
  75.         aLine.last.y = p->y;
  76.         
  77.         pWalk->currentPoint.x = p->x;
  78.         pWalk->currentPoint.y = p->y;
  79.         
  80.         GXDrawLine(&aLine);
  81.         
  82.         return(false);
  83.     }
  84.  
  85.  
  86.  
  87. Boolean    TestCurveTo(gxPoint p[3], TestWalkRec* pWalk);
  88. Boolean    TestCurveTo(gxPoint p[3], TestWalkRec* pWalk)
  89.     {
  90.         gxCurve        aCurve;
  91.         
  92.         aCurve.first.x = pWalk->currentPoint.x;
  93.         aCurve.first.y = pWalk->currentPoint.y;
  94.         
  95.         aCurve.control.x = p[1].x;
  96.         aCurve.control.y = p[1].y;
  97.         
  98.         aCurve.last.x = p[2].x;
  99.         aCurve.last.y = p[2].y;
  100.         
  101.         pWalk->currentPoint.x = p[2].x;
  102.         pWalk->currentPoint.y = p[2].y;
  103.         
  104.         GXDrawCurve(&aCurve);
  105.         
  106.         return(false);
  107.     }
  108.  
  109.  
  110.  
  111. Boolean TestClosePath( TestWalkRec* pWalk);
  112. Boolean TestClosePath( TestWalkRec* pWalk)
  113.     {
  114.         gxLine            aLine;
  115.         
  116.         aLine.first.x = pWalk->firstPoint.x;
  117.         aLine.first.y = pWalk->firstPoint.y;
  118.         aLine.last.x = pWalk->currentPoint.x;
  119.         aLine.last.y = pWalk->currentPoint.y;
  120.         
  121.         pWalk->currentPoint.x = pWalk->firstPoint.x;
  122.         pWalk->currentPoint.y = pWalk->firstPoint.y;
  123.         
  124.         GXDrawLine(&aLine);
  125.  
  126.         return(false);
  127.     }
  128.  
  129.  
  130. Fixed        ClGetFixedShapeLength(gxShape theShape, Boolean useGXMath)
  131.     {
  132.         wide            wLength;
  133.         Fixed            pathLength;
  134.         
  135.         if (useGXMath)
  136.             GXGetShapeLength(theShape, 0, &wLength);
  137.         else
  138.             AccurateGetShapeLength(theShape, 0, &wLength);
  139.             
  140.         pathLength = (Fixed)wLength.lo;
  141.     
  142.         return(pathLength);
  143.     
  144.     }//ClGetFixedShapeLength
  145.     
  146.  
  147. main() {
  148.  
  149.     Boolean            finishedEarly;
  150.     gxPoint            where = {ff(100), ff(300)};
  151.     gxShape            testPath;
  152.     TestWalkRec    walker;
  153.     Fixed                theLen, testLen, step;
  154.     gxPoint            location, tangent;
  155.     gxLine            aLine;
  156.     gxColor            aColor;
  157.     long                tix, oldTime, newTime;
  158.     
  159.     testPath = MakeThePath();
  160.     
  161.     aColor.space = gxRGBSpace;
  162.     aColor.profile = nil;
  163.     aColor.element.rgb.red = 0xFFFF;
  164.     aColor.element.rgb.green = 0;
  165.     aColor.element.rgb.blue = 0;
  166.     GXSetShapeColor(testPath, &aColor);
  167.     GXDrawShape(testPath);
  168.  
  169. #define DRAWTHEM
  170. #define NUMBEROFSTEPS 25
  171.     
  172.     theLen = ClGetFixedShapeLength(testPath, false);
  173.     
  174.     #ifndef DRAWTHEM
  175.         printf("Accurate length: %f\r\n", fix2float(theLen));
  176.     #endif
  177.     
  178.     step = FixedDivide(theLen, ff(NUMBEROFSTEPS));
  179.     tix = TickCount();
  180.     for (testLen = ff(0); testLen <= theLen; testLen += step) {
  181.  
  182.         (void)AccurateShapeLengthToPoint(testPath, 0, testLen, &location, &tangent);
  183.         
  184.         #ifdef DRAWTHEM
  185.             tangent.x = FixedMultiply(tangent.x, ff(5));
  186.             tangent.y = FixedMultiply(tangent.y, ff(5));
  187.             
  188.             aLine.first.x = location.x;
  189.             aLine.first.y = location.y;
  190.             aLine.last.x = location.x - FixedMultiply(ff(5), tangent.y);
  191.             aLine.last.y = location.y + FixedMultiply(ff(5), tangent.x);
  192.             
  193.             GXDrawLine(&aLine);
  194.         #endif
  195.     
  196.     }//end for
  197.     newTime = TickCount() - tix;
  198.  
  199.     /* Now do it the Built in Skia way */
  200.     //GXMoveShape(testPath, ff(0), ff(300));
  201.     //GXDrawShape(testPath);
  202.     
  203.     theLen = ClGetFixedShapeLength(testPath, true);
  204.  
  205.     #ifndef DRAWTHEM
  206.         printf("GX Calculated length: %f\r\n", fix2float(theLen));
  207.     #endif
  208.     
  209.     step = FixedDivide(theLen, ff(NUMBEROFSTEPS));
  210.     {
  211.         gxColor        aColor;
  212.         aColor.space = gxRGBSpace;
  213.         aColor.profile = nil;
  214.         aColor.element.rgb.red = 0x0000;
  215.         aColor.element.rgb.green = 0xFFFF;
  216.         aColor.element.rgb.blue = 0xFFFF;
  217.         GXSetInkColor(GXGetShapeInk(GXGetDefaultShape(gxLineType)), &aColor );
  218.     }
  219.  
  220.     tix = TickCount();
  221.     for (testLen = ff(0); testLen <= theLen; testLen += step) {
  222.  
  223.         (void)GXShapeLengthToPoint(testPath, 0, testLen, &location, &tangent);
  224.         
  225.         #ifdef DRAWTHEM
  226.             tangent.x = FixedMultiply(tangent.x, ff(5));
  227.             tangent.y = FixedMultiply(tangent.y, ff(5));
  228.             
  229.             aLine.first.x = location.x;
  230.             aLine.first.y = location.y;
  231.             aLine.last.x = location.x + FixedMultiply(ff(5), tangent.y);
  232.             aLine.last.y = location.y - FixedMultiply(ff(5), tangent.x);
  233.             
  234.             GXDrawLine(&aLine);
  235.         #endif
  236.     
  237.     }//end for
  238.     
  239.     
  240.     #ifndef DRAWTHEM
  241.         oldTime = TickCount() - tix;
  242.         printf("Time for old: %ld\r\n", oldTime);
  243.         printf("Time for new: %ld\r\n", newTime);
  244.     #endif
  245.             
  246.     GXDisposeShape(testPath);
  247.  
  248. }
  249.